package com.x.erp.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.x.erp.model.Apk;
import com.x.erp.rpc.ApkRpcService;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import com.x.framework.remote.HttpClient;

/**
 * app安装包版本信息
 */
@RestController
public class ApkController extends BaseController{

	@Reference
	private ApkRpcService apkRpcService;//app安装包版本信息服务

	@Autowired
	private HttpClient httpClient;

	/**
	 * 查询app安装包版本信息列表
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getApkList", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getApkList(Apk apk) {
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", apkRpcService.selectApkList(apk));
	}

	/**
	 * 查询app安装包版本信息数量
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getApkNum", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getApkNum(Apk apk) {
		Map<String, Object> res = new HashMap<>();
		res.put("total", apkRpcService.selectApkNum(apk));
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}

	/**
	 * 根据ID查询app安装包版本信息
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/getApk", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getApk(Apk apk) {
		if(apk == null || StringUtils.isEmpty(apk.getApkKey())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "ApkKey不能为空！");
		}
		if(StringUtils.isEmpty(apk.getApkVersion())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "版本号不能为空！");
		}
		String iosAppStoreVersion = apk.getApkVersion();
		//查询app当前版本
		List<Apk> apks = apkRpcService.selectApkList(apk);
		if(apks != null && apks.size() > 0) {
			apk = apks.get(0);
		}
		//查询app最新版本
		Apk maxApk = new Apk();
		maxApk.setApkKey(apk.getApkKey());
		apks = apkRpcService.selectApkList(maxApk);
		if(apks != null && apks.size() > 0) {
			if(apk.getApkKey().equals("anpxdIOSErpApp")) {
				String res = "";
				try {
					Map<String, Object> paramMap = new HashMap<>();
					paramMap.put("id", "1352712474");
					res = httpClient.doPostMap("https://itunes.apple.com/lookup", paramMap);
					if(StringUtils.isNotEmpty(res)) {
						iosAppStoreVersion = JSON.parseObject(res).getJSONArray("results").getJSONObject(0).getString("version");
					}
				} catch (Exception e) {
					logger.error("请求appStore出错：" + res, e);
				}
				for (Apk iosApk : apks){
					if(iosApk.getApkVersion().equals(iosAppStoreVersion)){
						maxApk = iosApk;
						if(apk.getApkVersion().equals(iosAppStoreVersion)){
							maxApk.setApkForce(0);
						} else {
							maxApk.setApkForce(apk.getApkForce());//当前版本是否需要强制更新
						}
						break;
					}
				}
			} else {
				maxApk = apks.get(0);
				maxApk.setApkForce(apk.getApkForce());//当前版本是否需要强制更新
			}
			return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", maxApk);
		}
		return new ResultModel(ResultCode.RESULT_FAIL, "查询失败");
	}

	/**
	 * 新增app安装包版本信息
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/insertApk", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel insertApk(Apk apk, String[] versionList) {
		if(apk == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "app安装包版本信息不能为空！");
		}
		if(StringUtils.isEmpty(apk.getApkKey())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "apkKey不能为空！");
		}
		if(StringUtils.isEmpty(apk.getApkName())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "名称不能为空！");
		}
		if(StringUtils.isEmpty(apk.getApkVersion())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "版本号不能为空！");
		}
		if(StringUtils.isEmpty(apk.getApkUrl())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "下载地址不能为空！");
		}
		if(valiVersion(apk)){
			return new ResultModel(ResultCode.RESULT_FAIL, "版本号不能重复！");
		}
		if(versionList != null && versionList.length > 0){
			selectVersion(versionList);
		}
		apk.setApkState(1);// 有效
		apk.setApkForce(0);// 不强制更新
		return new ResultModel(ResultCode.RESULT_SUCCESS, "插入成功！", apkRpcService.insertApk(apk));
	}

	/**
	 * 修改app安装包版本信息
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/updateApk", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel updateApk(Apk apk, String[] versionList) {
		if(apk == null) {
			return new ResultModel(ResultCode.RESULT_FAIL, "app安装包版本信息不能为空！");
		}
		if(StringUtils.isEmpty(apk.getApkKey())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "apkKey不能为空！");
		}
		if(StringUtils.isEmpty(apk.getApkName())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "名称不能为空！");
		}
		if(StringUtils.isEmpty(apk.getApkVersion())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "版本号不能为空！");
		}
		if(StringUtils.isEmpty(apk.getApkUrl())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "下载地址不能为空！");
		}
		Apk apk1 = new Apk();
		apk1.setApkKey(apk.getApkKey());
		apk1.setApkVersion(apk.getApkVersion());
		List<Apk> list = apkRpcService.selectApkList(apk1);
		if (list != null && list.size()>0 && !list.get(0).getApkId().equals(apk.getApkId())){
			if(valiVersion(apk)){
				return new ResultModel(ResultCode.RESULT_FAIL, "版本号不能重复！");
			}
		}

		if(versionList != null && versionList.length > 0){
			selectVersion(versionList);
		}
		apkRpcService.updateApk(apk);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "修改成功！");
	}

	/**
	 * 选择强制更新版本方法
	 * @param
	 * @return
	 */
	public void selectVersion(String[] versionList){
		Apk apk = new Apk();
		for (String version : versionList) {
			apk.setApkId(version);
			apk.setApkForce(1);
			apkRpcService.updateApk(apk);
		}
	}

	/**
	 * 验证版本是否重复方法
	 * @param
	 * @return
	 */
	public boolean valiVersion(Apk apk){
		Apk apk1 = new Apk();
		apk1.setApkKey(apk.getApkKey());
		apk1.setApkVersion(apk.getApkVersion());
		List<Apk> list = apkRpcService.selectApkList(apk1);
		if(list != null && list.size()>0){
			return true;
		}
		return false;
	}


}
